<script setup>
import { computed } from 'vue';

const props = defineProps(['list']);
const isAll = computed(() => {
  return props.list.length > 0 && props.list.every((v) => v.done);
});

const emit = defineEmits(['changeDone', 'del', 'changeAll']);
// 修改某一项
const changeDone = (e, id) => {
  emit('changeDone', { done: e.target.checked, id });
};

// 全选/反选
const changeAll = (e) => {
  // console.log('e.target.checked -----> ', e.target.checked);
  emit('changeAll', e.target.checked);
};
</script>

<template>
  <section class="main">
    <input
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
      :checked="isAll"
      @change="changeAll"
    />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <li :class="{ completed: item.done }" v-for="item in list" :key="item.id">
        <div class="view">
          <input
            class="toggle"
            type="checkbox"
            @change="changeDone($event, item.id)"
            :checked="item.done"
          />
          <label>{{ item.name }}</label>
          <button class="destroy" @click="emit('del', item.id)"></button>
        </div>
        <input class="edit" value="Create a TodoMVC template" />
      </li>
    </ul>
  </section>
</template>

<style lang="less" scoped></style>
